package com.book.bigwork.Webcontroller;

import com.book.bigwork.Webservice.BookService;
import com.book.bigwork.Webservice.OrderService;
import com.book.bigwork.Webservice.UserService;
import com.book.bigwork.model.Book;
import com.book.bigwork.model.Order;
import com.book.bigwork.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.security.Principal;
import java.util.List;

@Controller
public class OrderController {
    private final OrderService orderService;
    private final BookService bookService;
    private final UserService userService;

    @Autowired
    public OrderController(OrderService orderService, BookService bookService,
                           UserService userService) {
        this.orderService = orderService;
        this.bookService = bookService;
        this.userService = userService;
    }

    @GetMapping("/order") //查询订单信息
    public String getOrder(Model model) {
        model.addAttribute("order", orderService.getOrder());
        return "order";
    }

    @GetMapping("/historyOrder") //查询历史订单信息
    public String gethistoryOrder(Principal principal, Model model) {
        List<Order> orders = orderService.findByUserName
                (principal.getName());
        model.addAttribute("orders", orders);
        return "historyOrder";
    }

    @GetMapping("/confirmOrder") //订单信息确认
    public String showOrderConfirmInfo(Principal principal, Model model) {
        Order order = orderService.clearOrder();
        model.addAttribute("orders", order);
        return "orderConfirmInfo";

    }

    @GetMapping("/order/createOrder") //新增订单
    public String createOrder(Model model, Principal principal) {
        if (orderService.getOrder().getBooks().isEmpty()) {
            model.addAttribute("errorMessage", "订单为空");
            return getOrder(model);
        }
        orderService.createOrder(principal.getName());
        model.addAttribute("order", orderService.getOrder());
        User user = userService.findByUserName(principal.getName());
        model.addAttribute("user", user);
        return "checkout";
    }

    @GetMapping("/order/addBook/{bookId}") //添加图书到订单
    public String addBookToOrder(@PathVariable("bookId") Long bookId, Model
            model) {
        orderService.AddBookToOrder(bookService.findBookById(bookId));
        return getOrder(model);
    }

    @GetMapping("/order/removeBook/{bookId}") //从订单中删除图书
    public String removeBookFromOrder(@PathVariable("bookId") Long bookId,
                                      Model model) {
        Book bookDel = bookService.findBookById(bookId);
        orderService.removeBookFromOrder(bookDel);
        return getOrder(model);
    }

    @GetMapping("/orders/all") //查询所有订单信息
    @Secured("ROLE_ADMIN")
    public String getAllOrders(Model model) {
        List<Order> orders = orderService.findAllOrders();
        model.addAttribute("orders", orders);
        return "showAllOrders";
    }

    @GetMapping("/orders/delete/{orderId}") //删除订单信息
    @Secured("ROLE_ADMIN")
    public String deleteOrder(@PathVariable Long orderId, Model model) {
        Order order = orderService.findOrderById(orderId);
        orderService.deleteOrder(order);
        model.addAttribute("successMessage", "订单编号 " + orderId + " 删除成功");
        return getAllOrders(model);
    }

    @GetMapping("/order/{orderId}") //查询指定订单信息
    public String showOrderItem(@PathVariable Long orderId, Model model) {
        Order order = orderService.findOrderById(orderId);
        model.addAttribute("order", order);
        return "showOrderItem";
    }
}